package com.HW;

import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @ClassName : TIPAddress
 * @Author : kele
 * @Date: 2023/10/23 22:16
 * @Description :ipv4地址转为整数
 */
public class TIPAddressChange {

    public static void main(String[] args) {


        handle("100#101#1#5");
        handle("1#2#3");


    }

    public static void handle(String str) {

        //使用正则匹配
        String regex = "^((1[0-1]\\d|12[0-8]|[1-9]\\d?)#)((25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)#){2}(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)$";

        Pattern compile = Pattern.compile(regex);
        if (compile.matcher(str).matches()) {

//            Integer[] integers = Arrays.stream(str.split("#")).map(Integer::parseInt).toArray(Integer[]::new);
            int[] array = Arrays.stream(str.split("#")).mapToInt(Integer::parseInt).toArray();

            int res = (array[0] << 24) + (array[1] << 16) + (array[2] << 8) + array[3];
            System.out.println(res);
        }else{
            System.out.println("invalid IP");
        }


    }
}
